<?php

namespace app\modules\Contract\models;

use Yii;
use app\modules\Basic\models\Customer;
use app\modules\Manage\models\User;

/**
 * This is the model class for table "ai_contract".
 *
 * @property int $id
 * @property string $code 合同编号
 * @property int $type 1 内贸 2外贸 
 * @property int $customer_id 客户ID
 * @property int $sale_id 销售人员ID
 * @property string $sale_currency 合同币种
 * @property string $sale_price 合同金额
 * @property string $payment_type 收款方式
 * @property string $pay_currency 收款币种
 * @property string $pay_price 收款金额
 * @property string $exchange_currency 差额币种
 * @property string $exchange_price 汇兑差额
 * @property int $oper_id 商务操作ID
 * @property string $express_num 快递号
 * @property int $finance_id 财务ID
 * @property int $is_invoice 是否开票
 * @property string $invoice_date 开票日期
 * @property string $invoice_no 开票号
 * @property string $invoice_company 开票公司
 * @property string $invoice_currency 开票币种
 * @property string $invoice_price 开票金额
 * @property int $is_back 是否回款
 * @property string $back_date 回款日期
 * @property string $back_company 回款公司
 * @property string $back_currency 回款币种
 * @property string $back_price 回款金额
 * @property int $user_id 创建人ID
 * @property int $status 1提交 2已订货 3开票 4已发货 5回款 10删除
 * @property string $created_at 创建时间
 * @property string $updated_at
 */
class Contract extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'ai_contract';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['code'], 'required'],
            [['type', 'customer_id', 'sale_id', 'oper_id', 'finance_id', 'is_invoice', 'is_back', 'user_id', 'status','con_num'], 'integer'],
            [['sale_price', 'pay_price', 'exchange_price', 'invoice_price', 'back_price'], 'number'],
            [['invoice_date', 'back_date', 'created_at', 'updated_at'], 'safe'],
            [['code'], 'string', 'max' => 255],
            [['sale_currency', 'pay_currency', 'exchange_currency', 'invoice_currency', 'back_currency'], 'string', 'max' => 10],
            [['payment_type', 'express_num', 'invoice_no', 'invoice_company', 'back_company'], 'string', 'max' => 128],
        ];
    }

    //保存前处理
    public function beforeSave($insert) {//{{{

        if($insert){

            $this->created_at = date("Y-m-d H:i:s");
            $this->user_id    = Yii::$app->user->id;
        }
        return parent::beforeSave($insert);

    }//}}}

    //  获取客户信息
    public function getCustomer(){
        return $this->hasOne(Customer::className(), ['id' => 'customer_id']);
    }

    //获取销售
    public function getSaleName(){
        $user = User::findOne($this->sale_id);
        return $user ? $user->name : ' ';
    }

    //获取创建人
    public function getUserName(){
        $user = User::findOne($this->user_id);
        return $user ? $user->name : ' ';
    }


    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'code' => '合同编号',
            'type' => '合同类型',
            'customer_id' => '客户',
            'sale_id' => '销售员',
            'sale_currency' => '合同币种',
            'sale_price' => '合同金额',
            'payment_type' => '收款方式',
            'pay_currency' => '收款币种',
            'pay_price' => '收款金额',
            'exchange_currency' => '差额币种',
            'exchange_price' => '汇兑差额',
            'oper_id' => '商务操作',
            'express_num' => '快递号',
            'finance_id' => 'Finance ID',
            'is_invoice' => '是否开票',
            'invoice_date' => '开票日期',
            'invoice_no' => '开票号',
            'invoice_company' => '开票公司',
            'invoice_currency' => '开票币种',
            'invoice_price' => '开票金额',
            'is_back' => '是否回款',
            'back_date' => '回款日期',
            'back_company' => '回款公司',
            'back_currency' => '回款币种',
            'back_price' => '回款金额',
            'user_id' => '创建人',
            'status' => '合同状态',
            'created_at' => 'Created At',
            'updated_at' => 'Updated At',
        ];
    }
}
